﻿
using System;
using System.Windows.Input;
using System.Windows.Media;

namespace KinonekoSoftware.Foundation.UX.Controls
{
    public class DegreeControl : Control
    {

        public static readonly DependencyProperty ThumbGapProperty = DependencyProperty.Register(
            nameof(ThumbGap),
            typeof(int),
            typeof(DegreeControl),
            new FrameworkPropertyMetadata(2, FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty ThumbSizeProperty = DependencyProperty.Register(
            nameof(ThumbSize),
            typeof(int),
            typeof(DegreeControl),
            new FrameworkPropertyMetadata(16, FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
            nameof(Value),
            typeof(int),
            typeof(DegreeControl),
            new FrameworkPropertyMetadata(2, FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register(
            nameof(Maximum),
            typeof(int),
            typeof(DegreeControl),
            new FrameworkPropertyMetadata(10, FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register(
            nameof(Minimum),
            typeof(int),
            typeof(DegreeControl),
            new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register(
            nameof(Radius),
            typeof(double),
            typeof(DegreeControl),
            new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.AffectsRender));

        public double Radius
        {
            get => (double)GetValue(RadiusProperty);
            set => SetValue(RadiusProperty, value);
        }

        protected override void OnRender(DrawingContext dc)
        {
            if (Visibility != Visibility.Visible)
            {
                return;
            }
            
            //
            //
            dc.DrawRectangle(XamlCore.Transparent, null, new Rect(0, 0, ActualWidth, ActualHeight));

            
            var max  = Math.Clamp(Maximum, 0, 32);
            var max2 = max + 1;
            var min  = Math.Clamp(Minimum, 0, 32);
            var x    = 0d;
            
            
            if (min > max)
            {
                (max, min) = (min, max);
            }
            
            var v    = Math.Clamp(Value, min, max);
            var h    = ActualHeight;
            var gap  = Math.Clamp(ThumbGap, 2, 20);
            var size = Math.Clamp(ThumbSize, 10, 96);
            var bg   = Background ?? new SolidColorBrush(Colors.Gray);
            var fg   = (BorderBrush ?? Foreground) ?? new SolidColorBrush(Colors.Olive);
            var r    = Radius;
            
            //
            //
            for (var i = 1; i < max2; i++)
            {
                var b = v >= i ? fg : bg;
                
                //
                //
                dc.DrawRoundedRectangle(b, null, new Rect(x, 0, size, h), r, r);
                
                x += gap + size;

            }
            
            base.OnRender(dc);
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            var size  = Math.Clamp(ThumbSize, 10, 96) + ThumbGap;
            var p     = Math.Round(e.GetPosition(this).X);
            var index = (int)Math.Round(p / size);

            if (index == 1)
            {
                index = Value == 1 ? 0 : 1;
            }

            Value = index;
            base.OnMouseDown(e);
        }

        public int Minimum
        {
            get => (int)GetValue(MinimumProperty);
            set => SetValue(MinimumProperty, value);
        }
        public int Maximum
        {
            get => (int)GetValue(MaximumProperty);
            set => SetValue(MaximumProperty, value);
        }
        public int Value
        {
            get => (int)GetValue(ValueProperty);
            set => SetValue(ValueProperty, value);
        }

        public int ThumbSize
        {
            get => (int)GetValue(ThumbSizeProperty);
            set => SetValue(ThumbSizeProperty, value);
        }
        public int ThumbGap
        {
            get => (int)GetValue(ThumbGapProperty);
            set => SetValue(ThumbGapProperty, value);
        }
    }
}